/*
 * JobPoll.h
 *
 *   Created on: 2010-3-30     
 *  Description: 由Conn向之中添加Job，多线程执行，并回调conn.handle(WRITE)进行写入
 *    Copyright: 2010 @ ICT Li Heyuan
 */

#ifndef JOBPOLL_H_
#define JOBPOLL_H_

#include "Job.h"

#include <pthread.h>

#include <list>
using std::list;

class JobPoll
{
public:

	//向工作池中添加Job
	void addJob(Conn *conn);

	//创建单件类,n只对第一次有效
	static JobPoll* getInstance(unsigned int n = 1);

private:
	//单件类
	/* 创建默认n个worker线程的JobPoll
	 * @param n n个worker
	 */
	JobPoll(unsigned int n);
	virtual ~JobPoll();

	//worker的线程体
	static void* thread_func_worker(void *arg);

	//创建线程
	void initThreads();

public:
	enum
	{
		FINISH = 100
	//用于执行完method后，第一次写入
	};
private:
	//线程
	static JobPoll *_pInstance;

	//线程数量
	static unsigned int _numThreads;

	//存活的线程数量
	static unsigned int _numAliveThreads;

	//需要执行的rpcMethod的任务
	static list<Job> _jobs;

	//锁,同步_jobs
	static pthread_mutex_t _mutex_jobs;

	//条件变量
	static pthread_cond_t _cond_new_job;
};

void testJobPoll();

#endif /* JOBPOLL_H_ */
